{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程7.2】  模块创建及import指令运用\n",
    "\n",
    "Python 模块(Module)，是一个 Python 文件，以 .py 结尾，包含了 Python 对象定义和Python语句\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个模块，包含一个阶乘函数f1(n)、一个列表删值函数f2(lst,x),一个等差数列求和函数f3(a,d,n)\n",
    "\n",
    "def f1(n):\n",
    "    y = 1\n",
    "    for i in range(1,n+1):\n",
    "        y = y * i\n",
    "    return y\n",
    "# 创建阶乘函数f1(n)\n",
    "\n",
    "def f2(lst,x):\n",
    "    while x in lst:\n",
    "        lst.remove(x)\n",
    "    return lst\n",
    "# 创建列表删值函数f2(lst,x)\n",
    "\n",
    "def f3(a,d,n):\n",
    "    an = a\n",
    "    s = 0\n",
    "    for i in range(n-1):\n",
    "        an = an + d\n",
    "        s = s + an\n",
    "    return s\n",
    "# 创建等差数列求和函数f3(a,d,n)\n",
    "# 创建模块testmodel2，包括三个函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Anaconda3\\lib\\site-packages\\pandas\\__init__.py\n"
     ]
    }
   ],
   "source": [
    "# 模块路径问题\n",
    "\n",
    "import pandas\n",
    "print(pandas.__file__)\n",
    "# 查看现有包所在路径，将自己创建的包存入改路径\n",
    "\n",
    "import sys\n",
    "sys.path.append('C:/Users/Hjx/Desktop/')\n",
    "# 加载sys包，把新建的testmodel所在路径添加上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n",
      "[2, 3, 5, 5, 5, 6, 6]\n",
      "180\n"
     ]
    }
   ],
   "source": [
    "# 调用模块语句：import\n",
    "\n",
    "import testmodel2\n",
    "\n",
    "print(testmodel2.f1(5))\n",
    "print(testmodel2.f2([2,3,4,5,5,5,6,6,4,4,4,4],4))\n",
    "print(testmodel2.f3(10,2,10))\n",
    "# 直接用import调用模块，.f1()调用模块函数（方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n"
     ]
    }
   ],
   "source": [
    "# 简化模块名：import...as...\n",
    "\n",
    "import testmodel2 as tm2\n",
    "\n",
    "print(tm2.f1(5))\n",
    "# 简化模块名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 5, 5, 6, 6]\n"
     ]
    }
   ],
   "source": [
    "# 调用部分模块语句：From…import 语句\n",
    "\n",
    "from testmodel2 import f2\n",
    "\n",
    "print(f2([2,3,4,5,5,5,6,6,4,4,4,4],4))\n",
    "#print(f3(10,2,10))\n",
    "# 单独导入模块的部分功能，但无法使用其他未导入模块功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.44159988059787625 8.117099777940593\n",
      "6\n",
      "1 e\n",
      "[16, 13, 7, 5, 6]\n",
      "[9, 7, 3, 1, 13, 5, 11]\n"
     ]
    }
   ],
   "source": [
    "# python标准模块 —— random随机数\n",
    "\n",
    "import random\n",
    "\n",
    "x = random.random()\n",
    "y = random.random()\n",
    "print(x,y*10)\n",
    "# random.random()随机生成一个[0:1)的随机数\n",
    "\n",
    "m = random.randint(0,10)\n",
    "print(m)\n",
    "# random.randint()随机生成一个[0:10]的整数\n",
    "\n",
    "st1 = random.choice(list(range(10)))\n",
    "st2 = random.choice('abcdnehgjla')\n",
    "print(st1,st2)\n",
    "# random.choice()随机获取()中的一个元素，()种必须是一个有序类型\n",
    "\n",
    "lst = list(range(20))\n",
    "sli = random.sample(lst,5)\n",
    "print(sli)\n",
    "# random.sample(a,b)随机获取a中指定b长度的片段，不会改变原序列\n",
    "\n",
    "lst = [1,3,5,7,9,11,13]\n",
    "random.shuffle(lst)\n",
    "print(lst)\n",
    "# random.shuffle(list)将一个列表内的元素打乱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hello\n",
      "Fri Dec 15 17:17:47 2017\n",
      "<class 'str'>\n",
      "time.struct_time(tm_year=2017, tm_mon=12, tm_mday=15, tm_hour=17, tm_min=17, tm_sec=47, tm_wday=4, tm_yday=349, tm_isdst=0)\n",
      "<class 'time.struct_time'>\n",
      "2017-12-15 17:17:47\n"
     ]
    }
   ],
   "source": [
    "# python标准模块 —— time时间模块\n",
    "\n",
    "import time\n",
    "\n",
    "for i in range(2):\n",
    "    print('hello')\n",
    "    time.sleep(1)\n",
    "# time.sleep()程序休息()秒\n",
    "\n",
    "print(time.ctime())\n",
    "print(type(time.ctime()))\n",
    "# 将当前时间转换为一个字符串\n",
    "\n",
    "print(time.localtime())\n",
    "print(type(time.localtime()))\n",
    "# 将当前时间转为当前时区的struct_time\n",
    "# wday 0-6表示周日到周六\n",
    "# ydat 1-366 一年中的第几天\n",
    "# isdst 是否为夏令时，默认为-1\n",
    "\n",
    "print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))\n",
    "# time.strftime(a,b)\n",
    "# a为格式化字符串格式\n",
    "# b为时间戳，一般用localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%y 两位数的年份表示（00-99）\n",
    "\n",
    "%Y 四位数的年份表示（000-9999）\n",
    "\n",
    "%m 月份（01-12）\n",
    "\n",
    "%d 月内中的一天（0-31）\n",
    "\n",
    "%H 24小时制小时数（0-23）\n",
    "\n",
    "%I 12小时制小时数（01-12）\n",
    "\n",
    "%M 分钟数（00=59）\n",
    "\n",
    "%S 秒（00-59）\n",
    "\n",
    "%a 本地简化星期名称\n",
    "\n",
    "%A 本地完整星期名称\n",
    "\n",
    "%b 本地简化的月份名称\n",
    "\n",
    "%B 本地完整的月份名称\n",
    "\n",
    "%c 本地相应的日期表示和时间表示\n",
    "\n",
    "%j 年内的一天（001-366）\n",
    "\n",
    "%p 本地A.M.或P.M.的等价符\n",
    "\n",
    "%U 一年中的星期数（00-53）星期天为星期的开始\n",
    "\n",
    "%w 星期（0-6），星期天为星期的开始\n",
    "\n",
    "%W 一年中的星期数（00-53）星期一为星期的开始\n",
    "\n",
    "%x 本地相应的日期表示\n",
    "\n",
    "%X 本地相应的时间表示\n",
    "\n",
    "%Z 当前时区的名称\n",
    "\n",
    "%% %号本身\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
